import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:imclient/imclient.dart';
import 'package:imclient/model/conversation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sph_shipper/AppConfig/LocalStorage.dart';
import 'package:wfc_example/app_server.dart';
import 'package:wfc_example/config.dart';
import 'package:wfc_example/messages/messages_screen.dart';

import '../AppConfig/Config.dart';

class SingleChatPage extends StatefulWidget {
  const SingleChatPage({super.key, required this.uid});
  final String uid;
  @override
  State<SingleChatPage> createState() => _SingleChatPageState();
}

class _SingleChatPageState extends State<SingleChatPage> {
  @override
  late Conversation conversation;
  bool isLogin=false;
  void initState() {
    // TODO: implement initState
    super.initState();
    ImInit();
    print(widget.uid);
    if(widget.uid!="")
      conversation = Conversation(conversationType: ConversationType.Single, target:"${widget.uid}");



  }
//4ygqmws2k

  Future<void> ImInit() async {
    String? phoneNum =await PhoneNumManager.getPhone();
    AppServer.login(
        phoneNum!, "66666", (userId, token, isNewUser) {
      Imclient.connect(Config.IM_Host, userId, token);
      // Navigator.replace(context, oldRoute: ModalRoute.of(context)!,
      //     newRoute: MaterialPageRoute(builder: (context) => const HomeTabBar()));
      // SharedPreferences.getInstance().then((value) {
      //   value.setString("userId", userId);
      //   value.setString("token", token);
      //   value.commit();
      // });
      setState(() {
        isLogin=true;
      });
    }, (msg) {
      Fluttertoast.showToast(msg: "登录失败");
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      body: Container(
        child: isLogin? MessagesScreen(conversation):Container(),
      ),
    );
  }
}
